Profesor, MAF ITESO
Presentar los métodos básicos para la generación de números aleatorios uniformes y normales.
In [1]:
import numpy as np
import seaborn as sns
import scipy.stats as stats
%matplotlib inline
In [2]:
def lcg(n, m=2**31-1, a=16807, c=0, seed=2**30):
x = np.zeros(n+1)
x[0]=seed
for i in range(1,n+1):
x[i] = (a * x[i-1]+c)%m
return x[1:]/m
Ejemplo
In [3]:
lcg(10, m=31, a=13, c=0, seed=3)
Out[3]:
Generador mínimo estándar
In [4]:
x=lcg(10000)
sns.distplot(x, color="b", fit=stats.uniform);
Generado Randu (Usado por IBM)
In [5]:
x=lcg(10000, m=2**31, a=2**16+3, c=0, seed=3)
sns.distplot(x, color="b", fit=stats.uniform);
In [6]:
def bm(n):
m=2**31-1
a=16807
c=0
seed=2**30
x = np.zeros(n+1)
x[0]=seed
for i in range(1,n+1):
x[i] = (a * x[i-1]+c)%m
u=x[1:]/m
u1=u[:int((n/2))]
u2=u[int(n/2):]
nn=np.concatenate((np.sqrt(-2*np.log(1-u1))*np.cos(2*np.pi*u2), np.sqrt(-2*np.log(1-u1))*np.sin(2*np.pi*u2)),axis=0)
return nn
In [7]:
y=bm(100000)
sns.distplot(y, color="b", fit=stats.norm);
In [ ]: